#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<malloc.h>

typedef int STDataType;

typedef struct Stack {
    STDataType *a;
    int capacity;
    int top;
}ST;

void InitStack(ST *ps) {
    assert(ps);

    ps->a = (STDataType*)malloc(sizeof(STDataType)*4);
    ps->capacity = 4;
    ps->top = -1;
}

void Distory(ST *ps) {
    assert(ps);

    free(ps->a);
    ps->a = NULL;
    ps->capacity = ps->top = 0;
};

void StackPush(ST*ps,STDataType x) {
    assert(ps);

    if(ps->top+1 == ps->capacity) {
        STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType)*ps->capacity*2);
        ps->capacity*=2;
        if(tmp==NULL) {
            printf("realloc fail");
            exit(-1);
        }
        ps->a = tmp;
    }
    ps->a[++ps->top] = x;
};

void StackPop(ST *ps) {
    assert(ps);
    ps->top--;
};

int StackSize(ST*ps) {
    return ps->top+1;
};

bool StackEmpty(ST*ps) {
    return ps->top==-1;
};

STDataType StackTop(ST*ps) {
    return ps->a[ps->top];
};

int main() {
    ST l;
    ST*ps = &l;
    InitStack(ps);
    StackPush(ps,1);
    StackPush(ps,2);
    StackPop(ps);
    StackPush(ps,3);
    StackPop(ps);
    StackPush(ps,4);
    StackPush(ps,5);
    StackPush(ps,2);
    StackPush(ps,3);

    while(!StackEmpty(ps)) {
        printf("%d ",StackTop(ps));
        StackPop(ps);
    }
    printf("\n");

    Distory(ps);
    return 0;
}